package com.hisense.ovcloud.commons.configs.filters;


import lombok.extern.slf4j.Slf4j;

//@Component
@Slf4j
public class LogRequestResponseDataFilter {
//    implements
//} WebFilter {
//    private final DataBufferFactory dataBufferFactory = new NettyDataBufferFactory(ByteBufAllocator.DEFAULT);
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
//        long startTime = System.currentTimeMillis();
//        // 记录请求开始时间
//        //重写当前请求，打印请求返回参数，并统计接口耗时
//        return chain.filter(exchange.mutate().request(new ServerHttpRequestDecorator(exchange.getRequest()) {
//
//            @Override
//            public MultiValueMap<String, String> getQueryParams() {
//                log("request", "url: " + super.getURI() + ", params: " + super.getQueryParams());
//                return super.getQueryParams();
//            }
//
//            @Override
//            public Flux<DataBuffer> getBody() {
//                return super.getBody().map(dataBuffer -> dataBufferFactory.wrap(logDataBuffer("request", dataBuffer)));
//            }
//        }).response(new ServerHttpResponseDecorator(exchange.getResponse()) {
//            @Override
//            public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
//                return super.writeWith(logPublisher(body));
//            }
//
//            public Publisher<? extends DataBuffer> logPublisher(Publisher<? extends DataBuffer> body) {
//                if (body instanceof Flux) {
//                    return ((Flux<? extends DataBuffer>) body).map(dataBuffer -> bufferFactory().wrap(logDataBuffer("response", dataBuffer)));
//                } else if (body instanceof Mono) {
//                    return ((Mono<? extends DataBuffer>) body).map(dataBuffer -> bufferFactory().wrap(logDataBuffer("response", dataBuffer)));
//                }
//                return body;
//            }
//
//            @Override
//            public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
//                Publisher<? extends Publisher<? extends DataBuffer>> newBody = body;
//                if (body instanceof Flux) {
//                    newBody = ((Flux<? extends Publisher<? extends DataBuffer>>) body).map(publisher -> logPublisher(publisher));
//                } else if (body instanceof Mono) {
//                    newBody = ((Mono<? extends Publisher<? extends DataBuffer>>) body).map(publisher -> logPublisher(publisher));
//                }
//                return super.writeAndFlushWith(newBody);
//            }
//        }).build()).doOnSuccess(aVoid -> log.info("{} totally use: {} ms", exchange.getLogPrefix(), (System.currentTimeMillis() - startTime)));
//    }
//
//    private byte[] logDataBuffer(String prefix, DataBuffer dataBuffer) {
//        byte[] content = new byte[dataBuffer.readableByteCount()];
//        dataBuffer.read(content);
//        DataBufferUtils.release(dataBuffer);
//        String contentStr = new String(content, StandardCharsets.UTF_8);
//        log(prefix, contentStr);
//        return content;
//    }
//
//    private void log(String prefix, String contentStr) {
//        log.info("{} : {}", prefix, contentStr);
//    }
}

